package com.project.psc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.psc.bean.GoodsInfoBean;
import com.project.psc.bean.StoreBean;
import com.project.psc.mapper.IGoodsInfoMapper;
import com.project.psc.mapper.IStoreMapper;
import com.project.psc.service.IStoreService;
import com.project.psc.vo.AddGoodsVo;
import com.project.util.ConvertBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StoreServiceImpl implements IStoreService {
    @Autowired
    private IStoreMapper storeMapper;
    @Autowired
    private IGoodsInfoMapper goodsInfoMapper;

    @Override
    public IPage<StoreBean> cutByItem(Integer pageNO, String goodsName, String type, String brand, Long supplierId) {
        return storeMapper.cutByItem(new Page<>(pageNO, 6), goodsName, type, brand, supplierId);
    }

    @Override
    public AddGoodsVo findById(String goodsId, Long supplierId) {
        QueryWrapper<StoreBean> qw = new QueryWrapper<>();
        qw.eq("s_goodsId", goodsId);
        qw.eq("fk_supplierId", supplierId);
        StoreBean storeBean = storeMapper.selectOne(qw);
        if (storeBean != null) {
            AddGoodsVo copy = ConvertBeanUtil.copy(storeBean, AddGoodsVo.class);
            copy.setNum(1);
            copy.setStatus(true);
            return copy;
        }
        return null;
    }

    @Override
    public void delete(Long id) {
        storeMapper.deleteById(id);
        QueryWrapper<GoodsInfoBean> qw = new QueryWrapper<>();
        qw.eq("fk_storeId", id);
        goodsInfoMapper.delete(qw);
    }

    @Override
    public void add(AddGoodsVo goodsVo,Long supplierId) {
//       供应商没有该商品
        if (!goodsVo.isStatus()){
            StoreBean store = ConvertBeanUtil.copy(goodsVo, StoreBean.class);
            GoodsInfoBean goodsInfo = ConvertBeanUtil.copy(goodsVo, GoodsInfoBean.class);
            store.setSupplierId(supplierId);
            storeMapper.insert(store);
            goodsInfo.setStoreId(store.getId());
            goodsInfoMapper.insert(goodsInfo);
            return;
        }
   //供应商有该商品
        QueryWrapper<StoreBean> qw = new QueryWrapper<>();
        qw.eq("s_goodsId", goodsVo.getGoodsId());
        qw.eq("fk_supplierId", supplierId);
        StoreBean storeBean = storeMapper.selectOne(qw);
        storeBean.setNum(storeBean.getNum()+ goodsVo.getNum());
        GoodsInfoBean goodsInfo = ConvertBeanUtil.copy(goodsVo, GoodsInfoBean.class);
        goodsInfo.setStoreId(storeBean.getId());
        storeMapper.updateById(storeBean);
        goodsInfoMapper.insert(goodsInfo);
    }

    @Override
    public List<String> findAllType() {
        return storeMapper.findAllType();
    }

    @Override
    public List<String> findAllBrand() {
        return storeMapper.findAllBrand();
    }

    @Override
    public void updateNum(Long goodsInfoId, Integer num) {
        GoodsInfoBean goodsInfoBean = goodsInfoMapper.selectById(goodsInfoId);
        StoreBean storeBean = storeMapper.selectById(goodsInfoBean.getStoreId());
        Integer i = num - goodsInfoBean.getNum();//改变了多少数量
        goodsInfoBean.setNum(num);
        storeBean.setNum(storeBean.getNum() + i);
        goodsInfoMapper.updateById(goodsInfoBean);
        storeMapper.updateById(storeBean);
    }

    @Override
    public void deleteGoods(Long goodsInfoId) {
        GoodsInfoBean goodsInfoBean = goodsInfoMapper.selectById(goodsInfoId);
        StoreBean storeBean = storeMapper.selectById(goodsInfoBean.getStoreId());
        storeBean.setNum(storeBean.getNum() - goodsInfoBean.getNum());

        goodsInfoMapper.deleteById(goodsInfoId);
        storeMapper.updateById(storeBean);
    }
}
